package com.tcoiss.log.service.impl;

import com.tcoiss.log.domain.ApiLog;
import com.tcoiss.log.mapper.ApiLogMapper;
import com.tcoiss.log.service.IApiLogService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 接口调用日志Service业务层处理
 *
 * @author zw
 * @date 2021-05-07
 */
@Service
public class ApiLogServiceImpl extends ServiceImpl<ApiLogMapper, ApiLog> implements IApiLogService {

    @Override
    public List<ApiLog> queryList(ApiLog callApiLog) {
        LambdaQueryWrapper<ApiLog> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(callApiLog.getApiName())){
            lqw.like(ApiLog::getApiName ,callApiLog.getApiName());
        }
        if (StringUtils.isNotBlank(callApiLog.getApiCode())){
            lqw.eq(ApiLog::getApiCode ,callApiLog.getApiCode());
        }
        if(callApiLog.getStatus() != null){
            lqw.eq(ApiLog::getStatus,callApiLog.getStatus());
        }
        if (callApiLog.getCallTime() != null){
            lqw.gt(ApiLog::getCallTime ,callApiLog.getCallTime());
            Calendar c = Calendar.getInstance();
            c.setTime(callApiLog.getCallTime());
            c.add(Calendar.DATE,1);
            Date newDate = c.getTime();
            lqw.lt(ApiLog::getCallTime ,newDate);
        }
        lqw.orderByDesc(ApiLog::getCallTime);
        return this.list(lqw);
    }
}
